package com.googlecode.garbagecan.cxfstudy.utils;

import java.util.HashMap;
import java.util.Map;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;

public class ServerUtils {

	public static void publish(String address, Class<?> serviceClass) {
		JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
		factoryBean.getInInterceptors().add(new LoggingInInterceptor());
		factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());

		Map<String, Object> props = new HashMap<String, Object>();
		props.put("action", "UsernameToken");
		props.put("passwordType", "PasswordText");
		props.put("passwordCallbackClass", ServerUsernamePasswordHandler.class.getName());
		WSS4JInInterceptor wss4JInInterceptor = new WSS4JInInterceptor(props);
		factoryBean.getInInterceptors().add(wss4JInInterceptor);
		
		factoryBean.setServiceClass(serviceClass);
		factoryBean.setAddress(address);
		factoryBean.create();
	}
	
	public static void publish(String address, Object serviceBean) {
		JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
		factoryBean.getInInterceptors().add(new LoggingInInterceptor());
		factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());

		Map<String, Object> props = new HashMap<String, Object>();
		props.put("action", "UsernameToken");
		props.put("passwordType", "PasswordText");
		props.put("passwordCallbackClass", ServerUsernamePasswordHandler.class.getName());
		WSS4JInInterceptor wss4JInInterceptor = new WSS4JInInterceptor(props);
		factoryBean.getInInterceptors().add(wss4JInInterceptor);
		
		factoryBean.setServiceBean(serviceBean);
		factoryBean.setAddress(address);
		factoryBean.create();
	}
}
